﻿using TradePublish.Common;
using TradePublish.Exceptions;
using log4net;
using TradePublish.Service;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Reflection;
using System.Threading;
using System.Data.OracleClient;
namespace TradePublish.Dao
{
    public class BrandInfoDao
    {
        private ILog log = null;

        public BrandInfoDao()
        {
            this.log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        }

        public void saveBrandInfo(string[] detial, bool thisRegHasGrasped, int synchroflag)
        {
            while (true)
            {
                if (DateTime.Now.Hour == 6 && DateTime.Now.Minute < 30  || (DateTime.Now.Hour >=8 && DateTime.Now.Hour <18  &&  Convert.ToInt32(DateTime.Now.DayOfWeek) !=0 && Convert.ToInt32(DateTime.Now.DayOfWeek) !=6   ) )
                {
                    Thread.Sleep(180000);//3fenzhong  
                    continue;
                }
                break;
            }
            DatabaseHelper dbh = new DatabaseHelper();
            string cmdText = "";
            OracleParameter parSysp = new OracleParameter(":p1",OracleType.Clob);
            if (detial[0x15] == null || detial[0x15].Equals(""))
            {
                parSysp.Value = DBNull.Value;
            }
            else
            {
                parSysp.Value = "" + detial[0x15];
            }
            OracleParameter parSblc = new OracleParameter(":p2", OracleType.Clob); 
            if (detial[0x16] == null || detial[0x16].Equals(""))
            {
                parSblc.Value = DBNull.Value;
            }
            else
            {
                parSblc.Value = "" + detial[0x16];
            }
            OracleParameter parBz = new OracleParameter(":p3",OracleType.NVarChar );
            if (detial[0x10] == null || detial[0x10].Equals(""))
            {
                parBz.Value = DBNull.Value;
            }
            else
            {
                parBz.Value = "" + detial[0x10];
            } 
            try
            { 
                if (!thisRegHasGrasped)
                {
                    cmdText = "insert into   BRAND_INFO  (id,注册号,国际分类,商标文字_临时,"+
                    "申请日期,申请人名称_中文,申请人地址_中文,申请人2,"
                    +"申请人3,申请人名称_英文,申请人地址_英文,使用商品,"+
                    "初审公告期号,初审公告日期,注册公告期号,注册公告日期,"+
                    "专用权期限,代理人名称,是否共有商标,商标类型,商标流程,备注 ) ";
                    string str2 = cmdText;
                    cmdText = str2 + " values(gen_sequence.nextval,'" + detial[0x11] + "','" + detial[0] + "','" + detial[0x12].Replace("'", "''") + "',"
                        + (detial[1].Equals("") ? "null" : ("to_date('" + detial[1].Replace("'", "''") + "','yyyy-mm-dd')")) + ",'" + detial[2].Replace("'", "''") + "','" + detial[3].Replace("'", "''") + "','" + detial[0x13].Replace("'", "''") + "','" 
                        + detial[20].Replace("'", "''") + "','" + detial[4].Replace("'", "''") + "','" + detial[5].Replace("'", "''") + "',:p1,'" 
                        + detial[7].Replace("'", "''") + "'," + (detial[8].Equals("") ? "null" : ("to_date('" + detial[8].Replace("'", "''") + "','yyyy-mm-dd')")) + ",'" + detial[9].Replace("'", "''") + "'," + (detial[10].Equals("") ? "null" : ("to_date('" + detial[10] + "','yyyy-mm-dd')")) + ",'" 
                        + detial[11].Replace("'", "''") + "','" + detial[12].Replace("'", "''") + "','" + detial[13] + "','" + detial[14].Replace("'", "''") + "',:p2,:p3)";
                }
                else
                {
                    cmdText = string.Concat(new object[] { 
                    "update  BRAND_INFO   set synchroflag=", synchroflag, ",国际分类='", detial[0], "',商标文字_临时='", detial[0x12].Replace("'", "''"), "', 申请日期=", detial[1].Equals("") ? "null" : ("to_date('" + detial[1].Replace("'", "''") + "','yyyy-mm-dd')"), " ,申请人名称_中文='", detial[2].Replace("'", "''"), "',申请人地址_中文='", detial[3].Replace("'", "''"), "',申请人2='", detial[0x13].Replace("'", "''"), "',申请人3='", detial[20].Replace("'", "''"), 
                    "',申请人名称_英文='", detial[4].Replace("'", "''"), "',申请人地址_英文='", detial[5].Replace("'", "''"), "',使用商品=:p1,初审公告期号='", detial[7].Replace("'", "''"), "',初审公告日期=", detial[8].Equals("") ? "null" : ("to_date('" + detial[8].Replace("'", "''") + "','yyyy-mm-dd')"), ",注册公告期号='", detial[9].Replace("'", "''"), "',注册公告日期=", detial[10].Equals("") ? "null" : ("to_date('" + detial[10] + "','yyyy-mm-dd')"), ",专用权期限='", detial[11].Replace("'", "''"), 
                    "',代理人名称='", detial[12].Replace("'", "''"), "',是否共有商标='", detial[13], "',商标类型='", detial[14].Replace("'", "''"), "',商标流程=:p2,备注=:p3  where 注册号='"+ detial[0x11]+"'"
                 });
                }
                log.Debug(cmdText);
                dbh.BeginTransaction();
                dbh.AddParameter(parSysp);
                dbh.AddParameter(parSblc);
                dbh.AddParameter(parBz);
                dbh.ExecuteNonQuery(cmdText, TradePublish.Service.ConnectionState.KeepOpen);
                dbh.CommitTransaction();
            }
            catch (Exception exception)
            {
                log.Error("%%%%%%%%%%执行更新BRAND_INFO 报异常：" + cmdText + "\n使用商品:" + parSysp.Value + "\n商标流程:" + parSblc.Value + "\n备注:" + parBz.Value + exception.Message);
                dbh.RollbackTransaction();
                throw new GraspTMException("存入商标信息时,发生异常信息：" + cmdText + "\n使用商品:" + parSysp.Value + "\n商标流程:" + parSblc.Value + "\n备注:" + parBz.Value + exception.Message);
            }
            finally
            {
                dbh.Dispose();
            }
        }

        public void updateSylc(string regNo, string sylc)
        {
            string str = "update BRAND_INFO  set 商标流程='" + sylc.Replace("'", "''");
            while (true)
            {
                if (DateTime.Now.Hour == 6 && DateTime.Now.Minute < 30  || (DateTime.Now.Hour >=8 && DateTime.Now.Hour <18  &&  Convert.ToInt32(DateTime.Now.DayOfWeek) !=0 && Convert.ToInt32(DateTime.Now.DayOfWeek) !=6   ) )
                {
                    Thread.Sleep(180000);//3fenzhong  
                    continue;
                }
                 string str2 ="";
                try
                {
                    str2 = this.verifyHasGrasped(regNo);
                }
                catch(Exception  ex){
                    log.Error("查看商标是否存在时发生异常！"+ex.Message);
                    continue;
                }
                
                DatabaseHelper dbh = new DatabaseHelper(); 
                try
                {
                    if (!str2.Equals("00"))
                    {
                        string commandText = str + "',synchroflag=" + str2.Substring(1) + " WHERE 注册号 ='" + regNo + "'";
                        dbh.BeginTransaction();
                        dbh.ExecuteNonQuery(commandText, TradePublish.Service.ConnectionState.KeepOpen);
                        dbh.CommitTransaction();
                    }
                    return;
                }
                catch (Exception exception)
                {
                    dbh.RollbackTransaction();
                    this.log.Error("保存数据库表BRAND_INFO，设置商标流程异常", exception);
                    Thread.Sleep(0x1388);
                }
                finally
                {
                    dbh.Dispose();
                }
            }
        }

        public string verifyHasGrasped(string regNo)
        {
            while (true)
            {
                if (DateTime.Now.Hour == 6 && DateTime.Now.Minute < 30  || (DateTime.Now.Hour >=8 && DateTime.Now.Hour <18  &&  Convert.ToInt32(DateTime.Now.DayOfWeek) !=0 && Convert.ToInt32(DateTime.Now.DayOfWeek) !=6   ) )
                {
                    Thread.Sleep(180000);//3fenzhong  
                    continue;
                }
                break;
            }

            string str2;
            string commandText = " select ID ,synchroflag from BRAND_INFO  where 注册号='" + regNo + "'";
            DataSet set = new DataSet();
            DatabaseHelper dbh = new DatabaseHelper(); 
            try
            {
                dbh.BeginTransaction();
                set = dbh.ExecuteDataSet(commandText,TradePublish.Service.ConnectionState.KeepOpen);
                if ((((set != null) && (set.Tables != null)) && (set.Tables.Count > 0)) && (set.Tables[0].Rows.Count > 0))
                {
                    if (Convert.ToInt32(set.Tables[0].Rows[0][1]) == 0)
                    {
                        return "10";
                    }
                    return "12";
                }
                dbh.CommitTransaction(); 
                str2 = "00";
            }
            catch (Exception exception)
            {
                dbh.RollbackTransaction();
                log.Error(exception.Message);
                throw exception;
            }
            finally
            {
                dbh.Dispose();
            }
            return str2;
        }
    }
}